﻿@using CubicFWNet.Data.ViewModal
@model CubicFWNet.Data.Models.PurchaseCompose
@*@model IEnumerable<ProductTechnologyView>*@
@{
    //ViewBag.Title = "价格核算";
    var pro = ViewBag.Product as CubicFWNet.Data.Models.Product;
    var customer = ViewBag.Customer as CubicFWNet.Data.Models.Customer;
    var pvList = ViewBag.PvList as List<ProductTechnologyView>;
    var operationTime = ViewBag.OperationTime;
    Layout = "~/Views/Shared/_InfoPage.cshtml";
}

<section class="content">
    <div class="box box-solid">
        <!-- /.box-header -->
        <div class="box-body">
            <div class="row">
                @if (Model != null)
                {
                    <form class="form-horizontal" id="frmedit" action="@Url.Action("CompletePurchase", "Purchase")" method="post">
                        <div class="box-body">
                            <input type="hidden" id="Id" name="Id" value="@Model.Id" />
                            <input type="hidden" id="Status" name="Status" value="@Model.Status" />
                            <input type="hidden" id="SupplierId" name="SupplierId" value="@Model.SupplierId" />
                            <input type="hidden" id="ApplyTime" name="ApplyTime" value="@Model.ApplyTime" />
                            <input type="hidden" id="ProductId" name="ProductId" value="@Model.ProductId" />
                            <input type="hidden" id="Type" name="Type" value="@Model.Type" />
                            <input type="hidden" id="ApplyId" name="ApplyId" value="@Model.ApplyId" />@*申请人id*@
                            <div id="proTableListBox">
                                @if (pvList != null)
                                {
                                    foreach (var item in pvList)
                                    {
                                        <div class="col-md-12" style="border-bottom: 1px solid #ccc; margin-top: 8px;">
                                            <div class="form-group col-md-4">
                                                <label class="col-sm-5 control-label">订单号</label>
                                                <div class="col-sm-7" style="padding-top: 7px; margin-bottom: 0;">
                                                    <a href="javascript:void(0)" onclick="ShowModal('@item.OrderId')">@item.OrderNum</a>
                                                </div>
                                            </div>
                                            <div class="form-group col-md-4">
                                                <label class="col-sm-5 control-label">工艺</label>
                                                <div class="col-sm-7" style="padding-top: 7px; margin-bottom: 0;">
                                                    <a href="javascript:void(0)" class="technologyinfo" data-id="@item.TechnologyId">查看工艺</a>
                                                </div>
                                            </div>
                                            <div class="form-group col-md-4">
                                                <label class="col-sm-5 control-label">数量</label>
                                                <div class="col-sm-7" style="padding-top: 7px; margin-bottom: 0;">
                                                    <span>@item.Amount</span>
                                                </div>
                                            </div>
                                        </div>
                                        <div class="form-group col-md-12" style="height: 9px;"></div>
                                    }
                                    <div class="form-group col-md-12" style="height: 1px;"></div>
                                }
                            </div>
                            <div class="form-group col-md-6">
                                <label class="col-sm-3 control-label">产品名称</label>
                                <div class="col-sm-9">
                                    @if (Model.Type == 1 && pro != null)
                                    {
                                        <input type="text" class="form-control" id="ProductName" name="ProductName" value="@pro.Name" readonly="readonly" />
                                    }
                                    else
                                    {
                                        <input type="text" class="form-control" id="PurchaseName" name="PurchaseName" value="@Model.ProductName" readonly="readonly" />
                                    }

                                </div>
                            </div>
                            <div class="form-group col-md-6">
                                <label class="col-sm-3 control-label">采购单号</label>
                                <div class="col-sm-9">
                                    <input type="text" class="form-control" id="Number" name="Number" value="@Model.Number" readonly="readonly" />
                                </div>
                            </div>
                            <div class="form-group col-md-6">
                                <label class="col-sm-3 control-label">供应商</label>
                                <div class="col-sm-9">
                                    @if (customer != null)
                                    {
                                        <input type="text" class="form-control" id="SupplierName" name="SupplierName" value="@customer.Name" readonly="readonly" />
                                    }
                                    else
                                    {
                                        <input type="text" class="form-control" id="SupplierName" name="SupplierName" value="无供应商" readonly="readonly" />
                                    }
                                </div>
                            </div>
                            <div class="form-group col-md-6">
                                <label class="col-sm-3 control-label">采购数量</label>
                                <div class="col-sm-9">
                                    <input type="text" class="form-control okeydown" id="Amount" name="Amount" value="@Model.Amount" readonly="readonly" />
                                </div>
                            </div>
                            <div class="form-group col-md-6">
                                <label class="col-sm-3 control-label">成本价</label>
                                <div class="col-sm-9">
                                    <input type="text" class="form-control okeydown" id="CostPrice" name="CostPrice" value="@Model.CostPrice" readonly="readonly" />
                                </div>
                            </div>
                            <div class="form-group col-md-6">
                                <label class="col-sm-3 control-label">发票号</label>
                                <div class="col-sm-9">
                                    @if (string.IsNullOrEmpty(Model.FinanceNumber))
                                    {
                                        <input type="text" class="form-control okeydown" id="FinanceNumber" name="FinanceNumber" value="" readonly="readonly" />
                                    }
                                    else
                                    {
                                        <input type="text" class="form-control okeydown" id="FinanceNumber" name="FinanceNumber" value="@Model.FinanceNumber" readonly="readonly" />
                                    }
                                </div>
                            </div>

                            <div class="form-group col-md-6" style="display: none">
                                <label class="col-sm-3 control-label">实际价格</label>
                                <div class="col-sm-9">
                                    <input type="text" class="form-control okeydown" id="ActualPrice" name="ActualPrice" value="@Model.CostPrice" readonly="readonly" />
                                </div>
                            </div>


                            <div class="form-group col-md-12" style="height: 1px;"></div>
                            @*@if (Model.Type == 1 && pro != null)*@
                            @if (pro != null)
                            {
                                <div class="form-group col-md-6">
                                    <label class="col-sm-3 control-label">长度-mm</label>
                                    <div class="col-sm-9">
                                        <input type="text" class="form-control" id="SizeLenth" name="SizeLenth" value="@pro.SizeLenth" readonly="readonly" />
                                    </div>
                                </div>
                                <div class="form-group col-md-6">
                                    <label class="col-sm-3 control-label">宽度-mm</label>
                                    <div class="col-sm-9">
                                        <input type="text" class="form-control" id="SizeWidth" name="SizeWidth" value="@pro.SizeWidth" readonly="readonly">
                                    </div>
                                </div>
                                <div class="form-group col-md-6">
                                    <label class="col-sm-3 control-label">基本单位</label>
                                    <div class="col-sm-9">
                                        <input type="text" class="form-control" id="Unit" name="Unit" value="@pro.Unit" readonly="readonly">
                                    </div>
                                </div>
                                <div class="form-group col-md-6">
                                    <label class="col-sm-3 control-label">包装单位</label>
                                    <div class="col-sm-9">
                                        <input type="text" class="form-control" id="Pack" name="Pack" value="@pro.Pack" readonly="readonly">
                                    </div>
                                </div>
                                <div class="form-group col-md-12" style="height: 1px;"></div>
                            }
                            <input type="hidden" id="modalData" value="@Model.Data" readonly="readonly">
                            <div id="json-content">
                            </div>
                            <div class="form-group col-md-12" style="height: 1px;"></div>
                            <div class="form-group col-md-6">
                                <label class="col-sm-3 control-label">备注</label>
                                <div class="col-sm-9">
                                    <textarea class="form-control" rows="3" placeholder="请输入 ..." name="Memo" id="Memo" readonly="readonly">@Model.Memo</textarea>
                                </div>
                            </div>
                            <div class="form-group col-md-12" style="height: 1px;"></div>
                            <div class="form-group col-md-6">
                                <label for="operationName" class="col-sm-3 control-label">操作人</label>
                                <div class="col-sm-9">
                                    <input type="text" class="form-control" id="operationName" name="operationName" value="@Model.CreatorName" readonly="readonly">
                                </div>
                            </div>
                            <div class="form-group col-md-6">
                                <label for="operationTime" class="col-sm-3 control-label">日期</label>
                                <div class="col-sm-9">
                                    <input type="text" class="form-control" id="operationTime" name="operationTime" value="@operationTime" readonly="readonly">
                                </div>
                            </div>
                        </div>
                    </form>
                }
            </div>
        </div>
    </div>
</section>
<div id="loadingPage" style="display: none;">
    <div class="loading-shade"></div>
    <div class="loading-content" onclick="$.loading(false)">数据加载中，请稍后…</div>
</div>

<style>
    .has-error {
        border-color: #dd4b39;
        box-shadow: none;
    }
</style>


@section Scripts{
    <script type="text/javascript">
        $(document).ready(function () {
            $("#frmedit").validate({
                submitHandler: function (form) {
                    var postData = $(form).formSerialize();
                    postData.Data = getParam();
                    $.submitForm({
                        url: $(form).attr("action"),
                        param: postData,
                        success: function () {
                            var status = document.getElementById("Status").value;
                            $.modalAlert("操作成功", "success");
                            if (status == "10") {
                                window.location.href = '@Url.Action("PurQueryOk", "Purchase")';
                            } else {
                                window.location.href = '@Url.Action("PurQuery", "Purchase")';
                            }
                        }
                    });
                }
            });

            var yy = document.getElementById("modalData").value;
            if (yy != "") {
                var status = document.getElementById("Status").value;
                var data = JSON.parse(document.getElementById("modalData").value);
                var $content = $('#json-content');
                var html = "";
                data.forEach(function (e, i) {
                    if (typeof e.value == "undefined") {
                        e.value = "";
                    }
                    if (typeof e.name == "undefined") {
                        e.name = "";
                    }
                    if (typeof e.des == "undefined") {
                        e.des = "未知";
                    }
                    if (status == "5") {
                        html += "<div class='form-group col-md-6'>";
                        html += "<label for='j_a' class='col-sm-3 control-label'>" + e.des + "</label>";
                        html += "<div class='col-sm-9'>";
                        html += "<input type='text' class='form-control' data-des='" + e.des + "' id='" + e.name + "' name='" + e.name + "' value='" + e.value + "' readonly='readonly'>";
                        html += "</div></div>";
                    } else {
                        html += "<div class='form-group col-md-6'>";
                        html += "<label for='j_a' class='col-sm-3 control-label'>" + e.des + "</label>";
                        html += "<div class='col-sm-9'>";
                        if (e.name == "ArrivalTime") {
                            if (status == "10") {
                                html += "<input type='text' class='form-control Timedatepicker' data-des='" + e.des + "' id='" + e.name + "' name='" + e.name + "' value='" + e.value + "' readonly='readonly'>";
                            } else {
                                html += "<input type='text' class='form-control Timedatepicker' data-des='" + e.des + "' id='" + e.name + "' name='" + e.name + "' value='" + e.value + "' required>";
                            }
                        } else {
                            html += "<input type='text' class='form-control' data-des='" + e.des + "' id='" + e.name + "' name='" + e.name + "' value='" + e.value + "' readonly='readonly'>";
                        }

                        html += "</div></div>";
                    }

                });
                $content.append(html);
            }


            //日期控件
            $(".Timedatepicker").datepicker({
                autoclose: true,
                language: 'zh', //汉化
                format: "yyyy-mm-dd", //选择日期后，文本框显示的日期格式
                minView: "month"//选择日期后，不会再跳转去选择时分秒
            });
        });

        function formsubmit(t) {
            var value = $("#SupplierId").val();
            //var arrivalAmount = $("#ArrivalAmount");
            //var amount = $("#Amount");
            if (value == "" || value == "0") {
                $.modalAlert("请选择供应商", "warning");
                return false;
            }
            //if (arrivalAmount > amount) {
            //    $.modalAlert("到货数量不能大于采购数量", "warning");
            //    return false;
            //}
            $("#frmedit").submit();
        }

        $(".okeydown").on("keyup", function () {
            var value = $(this).val();
            if (isNaN(value) || value == "" || value == "Infinity" || value == "NaN") {
                $(this).addClass("has-error");
            } else {
                $(this).removeClass("has-error");
            }
        });

        $("#historyBack").click(function () {
            window.history.go(-1);  //返回上一页
            //window.history.back();  //返回上一页
        });

        $(".okeydown").on("keydown", function () {
            var k = event.keyCode;
            if ((k <= 57 && k >= 48) || (k <= 105 && k >= 96) || (k == 8) || (k == 110) || (k == 190)) {
                return true;
            } else {
                return false;
            }
        });

        //json-content
        function getParam() {
            var postdata = [];
            //主键
            $("#json-content").find('input,select,textarea').each(function (r) {
                var $this = $(this);
                var _des = $this.attr('data-des');
                var _id = $this.attr('id');
                var type = $this.attr('type');
                switch (type) {
                    case "checkbox":
                        postdata.push({ des: _des, name: _id, value: $this.is(":checked") });
                        break;
                    case "select":
                        var value = $this.find("option:selected").val() == "" ? null : $this.find("option:selected").val();
                        postdata.push({ des: _des, name: _id, value: value });
                        break;
                    default:
                        var value = $this.val() == "" ? "&nbsp;" : $this.val();
                        postdata.push({ des: _des, name: _id, value: value });
                        break;
                }

            });
            return JSON.stringify(postdata);
        }

        $(".technologyinfo").click(function () {
            var id = $(this).data('id');
            var options = {
                id: null,
                title: '产品信息',
                width: "100%",
                height: "100%",
                url: "/Technology/TechnologyInfo?tId=" + id,
                shade: 0.3,
                btn: ['返回'],
                btnclass: ['btn btn-danger'],
                callBack: null
            }
            $.modalOpen(options);
        });
        //订单信息弹窗
        function ShowModal(id) {
            var options = {
                id: null,
                title: '订单信息',
                width: "100%",
                height: "100%",
                url: "/Order/OrderInfo?OID=" + id,
                shade: 0.3,
                btn: ['返回'],
                btnclass: ['btn btn-danger'],
                callBack: null
            }
            $.modalOpen(options);
        }
    </script>
}